import { POP_CLOSE_TYPE, POP_SHOW_TYPE } from "../constants/Enums";

/**
 * 弹窗管理
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class PopUpManager extends cc.Component {
    private prefabs: any = {};
    private panelCache: any = {};
    private panels: cc.Node[] = [];
    private static _instance: PopUpManager;
    public static get Inst(): PopUpManager {
        if (!this._instance) this._instance = new PopUpManager();
        return this._instance;
    }
    /**
     * 开启界面
     * @param prefab 开启界面预制或者名称
     */
    openPanel(prefab: cc.Prefab | string, showType: number = POP_SHOW_TYPE.DEFAULT, data: any = null) {
        let panel: cc.Node = null;
        if (typeof prefab == "string") {
            if (this.panels.length && this.panels[this.panels.length - 1].name === prefab) return;//防止多次点击，重复添加
            if (this.prefabs[prefab]) {
                this.openPanel(this.prefabs[prefab], showType, data);
            } else {
                cc.resources.load("prefab/" + prefab, cc.Prefab, (err, resource) => {
                    if (err) return console.error(err);
                    this.prefabs[prefab] = resource;
                    this.openPanel(resource, showType, data);
                });
            }
        } else {
            let key = prefab.name;
            if (this.panels.length && this.panels[this.panels.length - 1].name === key) return;//防止多次点击，重复添加
            if (this.panelCache[key]) {
                let caches: cc.Node[] = this.panelCache[key];
                if (caches.length) {
                    panel = caches.pop();
                }
            }
            if (!panel) {
                panel = cc.instantiate(prefab);
                panel.name = key;
                panel.getChildByName("content").opacity = 0;//避免一闪而过
            }
            this.panels.push(panel);
            if (panel.parent)
                panel.parent.removeChild(panel);
            cc.director.getScene().getChildByName('Canvas').getChildByName('popNode').addChild(panel, 1000);
            // this.scheduleOnce(() => {
            let bp = panel.getComponent(key);
            if (bp && bp.showPanel)
                bp.showPanel(data, showType);
            // }, 0);
            // console.log(this.panelCache)
            // console.log(this.panels)
        }
    }
    /**开启提示 */
    openPrompt(key: string, callBack: Function = null, clickBg: Boolean = false) {
        cc.resources.load("prefab/PromptPanel", cc.Prefab, (err, resource) => {
            if (err) return console.error(err);
            let panel: cc.Node = null;
            if (this.panelCache["PromptPanel"]) {
                let caches: cc.Node[] = this.panelCache["PromptPanel"];
                if (caches.length) {
                    panel = caches.pop();
                }
            }
            if (!panel) {
                panel = cc.instantiate(resource);
                panel.name = "PromptPanel";
            }
            this.panels.push(panel);
            if (panel.parent)
                panel.parent.removeChild(panel);
            cc.director.getScene().getChildByName('Canvas').getChildByName('popNode').addChild(panel, 10000);
            let bp = panel.getComponent("PromptPanel");
            bp.showPrompt(key, callBack, clickBg);
            // console.log(this.panelCache)
            // console.log(this.panels)
        });
    }
    /**
     * 关闭
     */
    close(panel: cc.Node) {
        let callBack = (panel: cc.Node) => {
            this.clearPanel(panel);
        }
        let bp = panel.getComponent(panel.name);
        if (bp && bp.closePanel) {
            bp.closePanel(callBack);
        } else {
            callBack(panel);
        }
    }
    clearPanel(panel: cc.Node) {
        if (panel.parent)
            panel.parent.removeChild(panel);
        let key = panel.name;
        //清除
        for (let i = 0, len = this.panels.length; i < len; i++) {
            if (this.panels[i] == panel) {
                this.panels.splice(i, 1);
                break;
            }
        }
        if (!this.panelCache[key]) this.panelCache[key] = [];
        this.panelCache[key].push(panel);
        //恢复上一个
        if (this.panels.length) {
            let last = this.panels[this.panels.length - 1];
            if (last.active) return;
            let bp = last.getComponent(last.name);
            if (bp && bp.showPanel) {
                bp.showPanel(bp.data, bp.showType);
            } else {
                if (panel.parent)
                    panel.parent.removeChild(panel);
                cc.director.getScene().getChildByName('Canvas').getChildByName('popNode').addChild(panel, 1000);
            }
        }
        // console.log(this.panelCache)
        // console.log(this.panels)
    }
}
